/**
 * 作者：chenda
 * 时间：2019/1/2:15:41
 * 邮箱：
 * 说明：
 */

#include <jni.h>
#include "usbhelper.h"

//extern "C"
//JNIEXPORT jint JNICALL
//Java_cd_usbhelper_UsbHelperClient_sendMessage__Landroid_hardware_usb_UsbDeviceConnection_2Landroid_hardware_usb_UsbEndpoint_2Lbyte_3_093_2I(
//        JNIEnv *env, jobject instance, jobject deviceConnection, jobject usbEpOut,
//        jbyteArray send_cmd_, jint timeout) {
//
//    jbyte *send_cmd = (env)->GetByteArrayElements( send_cmd_, NULL);
//    jsize size = env->GetArrayLength(send_cmd_);
//    jclass clazz = env->GetObjectClass(deviceConnection);
//    jmethodID methodID = env->GetMethodID(clazz,"bulkTransfer","(Landroid/hardware/usb/UsbEndpoint;[BI)I");
//    int ret = env->CallIntMethod(deviceConnection,methodID,usbEpOut,send_cmd,size,timeout);
//    (env)->ReleaseByteArrayElements( send_cmd_, send_cmd, 0);
//    return ret;
//}


extern "C"
JNIEXPORT jint JNICALL
Java_cd_usbhelper_UsbHelperClient_readMessage(
        JNIEnv *env, jobject instance, jobject deviceConnection, jobject usbEpIn,
        jbyteArray response_cmd_, jint timeout) {
    jsize size = env->GetArrayLength(response_cmd_);
    jclass clazz = env->GetObjectClass(deviceConnection);
    jmethodID methodID = env->GetMethodID(clazz,"bulkTransfer","(Landroid/hardware/usb/UsbEndpoint;[BII)I");
    return env->CallIntMethod(deviceConnection,methodID,usbEpIn,response_cmd_,size,timeout);
}

extern "C"
JNIEXPORT jint JNICALL
Java_cd_usbhelper_UsbHelperClient_sendMessage(
        JNIEnv *env, jobject instance, jobject deviceConnection, jobject usbEpOut,
        jbyteArray send_cmd_, jint timeout) {
//    jbyte *send_cmd = (env)->GetByteArrayElements( send_cmd_, NULL);
    jsize size = env->GetArrayLength(send_cmd_);
    jclass clazz = env->GetObjectClass(deviceConnection);
    jmethodID methodID = env->GetMethodID(clazz,"bulkTransfer","(Landroid/hardware/usb/UsbEndpoint;[BII)I");

//    (env)->ReleaseByteArrayElements( send_cmd_, send_cmd, 0);
    return env->CallIntMethod(deviceConnection,methodID,usbEpOut,send_cmd_,size,timeout);
}

